//
//  YZMAllStockPacket.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/8.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMAllStockPacket.h"
#import "YZMStockItemModel.h"
#import "Mantle+Utils.h"

@interface YZMAllStockPacket()

@property (nonatomic) NSString *stockString;

@end

@implementation YZMAllStockPacket

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"stockString":@"stocks",
             @"versionDate": @"version",
             };
}

- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError **)error{
    
    self = [super initWithDictionary:dictionaryValue error:error];
    if (self == nil) return nil;
    
    NSString *string = dictionaryValue[@"stockString"];
    if (string.length != 0) {
        
        NSMutableArray *tempArray = [NSMutableArray array];
        NSArray *items = [string componentsSeparatedByString:@";"];
        for (NSString *subString in items) {
            
            NSArray *subItems = [subString componentsSeparatedByString:@","];
            YZMStockItemModel *stock = [YZMStockItemModel mapResponseArray:subItems];
            if (stock && stock.code.length != 0) {
                
                [tempArray addObject:stock];
            }
        }
        
        self.stocks = [NSArray arrayWithArray:tempArray];
    }
    
    return self;
}

+ (NSValueTransformer *)versionDateJSONTransformer
{
    return [NSValueTransformer valueTransformerForName: MTLDoubleDateTransformerName];
}

@end
